import {
	ref
} from 'vue'
import {
	setStore
} from '../../api/storage.js'
export let user = ref({
	phone: '',
	code: ''
})

export let toRegister = () => {
	uni.redirectTo({
		url: '/pages/register/register'
	});
}

export let login = () => {
	if (!user.value.phone || !user.value.code) {
		uni.showToast({
			title: '参数不能为空',
			icon: 'error',
			duration: 2000
		});
		return
	}
	uniCloud.callFunction({
			name: 'user',
			data: {
				apiName: 'login',
				params: user.value
			}
		})
		.then(async res => {
			console.log(res.result, '登录')
			if (!res.result.status) {
				uni.showToast({
					title: res.result.message,
					icon: 'error',
					duration: 2000
				});
				return
			}
			uni.showToast({
				title: res.result.message,
				icon: 'success',
				duration: 2000
			});
			// 写入token
			let token = res.result.token
			let restore = await setStore('token', token)
			// 重定向到首页
			uni.switchTab({
				url: '/pages/home/home'
			});
		});
}